package com.richapp.launcher.scene;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.ActionInfraredModel;
import com.richapp.launcher.database.system.mode.ActionModel;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.ConditionStatusModel;
import com.richapp.launcher.database.system.mode.ConditionTimeModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.actions.SceneCloseAction;
import com.richapp.launcher.scene.actions.SceneDimmingAction;
import com.richapp.launcher.scene.actions.SceneInfraredAction;
import com.richapp.launcher.scene.actions.SceneOpenAction;
import com.richapp.launcher.scene.actions.SceneStopAction;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorAirConditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorAlarmConditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorCo2Conditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorLightConditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorPm25Conditions;
import com.richapp.launcher.scene.conditions.sensor.SceneSensorTemperatureHumidityConditions;
import com.richapp.launcher.scene.conditions.status.SceneStatusCloseConditions;
import com.richapp.launcher.scene.conditions.status.SceneStatusDimmingConditions;
import com.richapp.launcher.scene.conditions.status.SceneStatusOpenConditions;
import com.richapp.launcher.scene.conditions.time.SceneTimeAgingConditions;
import com.richapp.launcher.scene.conditions.time.SceneTimeDelayConditions;
import com.richapp.launcher.scene.conditions.time.SceneTimeDimmingConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;

import android.content.Context;
import android.util.Log;

public class SceneServer {

    private Context context;
	private static SceneServer sceneServer = null;
	private boolean serverRunFlag = false;
	boolean	timeServerRunFlag = false;
	private boolean sceneOpenFlag = false;
	private Map<String,Scene> sceneMap = null;
	private Map<String,Scene> timeSceneMap = null;
	private Map<String,Scene> statusSceneMap = null;
	private Map<String,Scene> passtiveSceneMap = null;
    private Map<String,Scene> groupSceneMap = null;

	private  boolean checkScene=false;
	
	private static final String TAG = SceneServer.class.getName();
	
	private SceneServer() {
		sceneMap = new HashMap<String,Scene>();
		timeSceneMap = new HashMap<String,Scene>();
		statusSceneMap = new HashMap<String,Scene>();
		passtiveSceneMap = new HashMap<String,Scene>();
        groupSceneMap = new HashMap<String,Scene>();
	}
	
	public static SceneServer getInstance(){
		if(sceneServer == null){
			sceneServer = new SceneServer();
		}
		return sceneServer;
	}

	public void startStatusScene(){
//				serverRunFlag = true;
				//加载情景
//				Log.d(TAG, "加载状态情景......");
//					loadStatusList();//加载状态情景
//					startGroupScene();
//				Log.d(TAG, "加载状态情景完成");
//				while(sceneOpenFlag){
//					try {
//						for(Scene scene :statusSceneMap.values()) {
//							if (Scene.SCENE_TYPE_ACTIVE != scene.getSceneType()) {
//								if ("true".equals(scene.getOpenFlag())) {
//									Log.d(getClass().getName(), "检测状态情景：" + scene.getSceneName());
//									scene.checkCondition();
//								}
//							}
//						 }
//						stop();
//						Thread.sleep(3000);
//					} catch (Exception e) {
//						  stop();
//						if(e.getMessage()!=null){
//							Log.e(TAG, e.getMessage());
//						}
//					}
//				}
//				serverRunFlag = false;

	}


	public void startSensorScene(){
//				serverRunFlag = true;
				//加载情景
//				Log.d(TAG, "加载传感情景......");
//				startGroupScene();
/*			if (sceneMap.size()==0){
				loadSensorList();
			}*/
//				Log.d(TAG, "加载传感情景完成");
/*			while(sceneOpenFlag) {
				try {
					for (Scene scene : sceneMap.values()) {
//						if (scene.getSceneName()!=null && scene.getConditionList().size()!=0) {
							if (Scene.SCENE_TYPE_ACTIVE != scene.getSceneType()) {
								if ("true".equals(scene.getOpenFlag())) {
									Log.d(getClass().getName(), "检测传感情景：" + scene.getSceneName());
									scene.checkCondition();
								}
							}

					}
					stop();
//					Log.d("test", "情景服务测试,第" + (++count) + "次检测");
					stopGroupScene();
					Thread.sleep(2000);
				} catch (Exception e) {
					stop();
					if (e.getMessage() != null) {
						Log.e(TAG, e.getMessage());
					}
				}
				serverRunFlag = false;

			}*/
	}


	public void startTimeScene(){
		/*new Thread(new Runnable() {
			@Override
			public void run() {
				List<ISceneConditions> conditionList=null;
				timeServerRunFlag = true;
				//加载情景
				Log.d(TAG, "加载时间情景......");
//				loadTimeList();
				startGroupScene();
				if (timeSceneMap.size()==0){
					timeServerRunFlag=false;
				}
				Log.d(TAG, "加载时间情景完成");
				while(timeServerRunFlag){
					try {
						for(Scene scene :timeSceneMap.values()){
							conditionList=  scene.getConditionList();
							for (ISceneConditions ct:conditionList) {
								int	ConditionType=ct.getConditionType();
								if (scene.getSceneName()!=null && ConditionType==0) {
									if(Scene.SCENE_TYPE_ACTIVE != scene.getSceneType()) {
										if ("true".equals(scene.getOpenFlag())) {
											Log.d(getClass().getName(),"检测时间情景："+ scene.getSceneName());
											scene.checkCondition();
										}
									}
								}
							}
						}
						//Log.d("test", "情景服务测试,第"+(++count)+"次检测");
						Thread.sleep(1000*59);
					} catch (Exception e) {
						if(e.getMessage()!=null){
							Log.e(TAG, e.getMessage());
						}
					}
				}
				timeServerRunFlag = false;
			}
		}).start();*/
	}


	public void startGroupScene(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				serverRunFlag = true;
				//加载情景
				Log.d(TAG, "加载组合情景......");
				loadSceneList();
				Log.d(TAG, "加载组合情景完成");
				while(sceneOpenFlag){
					try {
						for(Scene scene :groupSceneMap.values()){
								if (Scene.SCENE_TYPE_ACTIVE != scene.getSceneType()) {
									if ("true".equals(scene.getOpenFlag())) {
										Log.d(getClass().getName(), "------检测情景：" + scene.getSceneName());
										scene.checkCondition();
									}
							}
						}
						Thread.sleep(1000);
					} catch (Exception e) {
						if(e.getMessage()!=null){
						Log.e(TAG, e.getMessage());
						}
					}
				}
				serverRunFlag = false;
			}
		}).start();
	}

	
/*	public void start(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				serverRunFlag = true;
				long count = 0;
				//加载情景
				Log.d(TAG, "加载情景......");
				loadSceneList();
				Log.d(TAG, "加载情景完成");
				while(sceneOpenFlag){
					try {
						for(Scene scene :sceneMap.values()){
							Log.d(getClass().getName(),"检测情景："+ scene.getSceneName());
							if(Scene.SCENE_TYPE_ACTIVE != scene.getSceneType()){
								if("true".equals(scene.getOpenFlag())){
									scene.checkCondition();
								}
							}
						}
						//Log.d("test", "情景服务测试,第"+(++count)+"次检测");
						Thread.sleep(1000*2);
					} catch (Exception e) {
						if(e.getMessage()!=null){
						Log.e(TAG, e.getMessage());
						}
					}
				}
				serverRunFlag = false;
			}
		}).start();
	}*/





	public void checkPasstiveScene(String sceneName){
		if(groupSceneMap.containsKey(sceneName)){
			groupSceneMap.get(sceneName).checkCondition();
		}
	}
	
	public void stop(){
		sceneOpenFlag = false;
		sceneMap.clear();
	}

    public void stopGroupScene(){
        sceneOpenFlag = false;
//        groupSceneMap.clear();
    }
	public void stopTimeScene(){
		sceneOpenFlag = false;
		timeSceneMap.clear();
	}

	public void stopStatusScene(){
		sceneOpenFlag = false;
		statusSceneMap.clear();
	}

	public Scene getScene(String sceneName){
		if(sceneMap.containsKey(sceneName)){
			return sceneMap.get(sceneName);
		}
		return null;
	}
	
	public void addScene(Scene scene){
		if(sceneMap.containsKey(scene.getSceneName())){
			sceneMap.remove(scene.getSceneName());
		}
		sceneMap.put(scene.getSceneName(),scene);
	}

    public void addGroupScene(Scene scene){
        if(groupSceneMap.containsKey(scene.getSceneName())){
            groupSceneMap.remove(scene.getSceneName());
        }
        groupSceneMap.put(scene.getSceneName(),scene);
    }

	public void addPasstiveSceneMap(Scene scene){
		if(passtiveSceneMap.containsKey(scene.getSceneName())){
			passtiveSceneMap.remove(scene.getSceneName());
		}
		passtiveSceneMap.put(scene.getSceneName(),scene);
	}

	public void addTimeScene(Scene scene){
		if(timeSceneMap.containsKey(scene.getSceneName())){
			timeSceneMap.remove(scene.getSceneName());
		}
		timeSceneMap.put(scene.getSceneName(),scene);
	}
	
	public void delScene(Scene scene){
		if(sceneMap.containsKey(scene.getSceneName())){
			sceneMap.remove(scene.getSceneName());
		}
	}

	public void addStatusScene(Scene scene){
		if(statusSceneMap.containsKey(scene.getSceneName())){
			statusSceneMap.remove(scene.getSceneName());
		}
		statusSceneMap.put(scene.getSceneName(),scene);
	}
	
	public void delScene(String sceneName){
		if(sceneMap.containsKey(sceneName)){
			sceneMap.remove(sceneName);
		}
	}

	public boolean isServerRunFlag() {
		return serverRunFlag;
	}

	public boolean isSceneOpenFlag() {
		return sceneOpenFlag;
	}

	public void setSceneOpenFlag(boolean sceneOpenFlag) {
		this.sceneOpenFlag = sceneOpenFlag;
	}

	public boolean isCheckScene() {
		return checkScene;
	}

	public void setCheckScene(boolean checkScene) {
		this.checkScene = checkScene;
	}
	
	public void loadSceneList(){
		groupSceneMap.clear();
		SceneModelDao smd = new SceneModelDao(context);
        List<SceneModel> sceneModelList = smd.getList();
		
		Scene scene = null;
		for(SceneModel sModel : sceneModelList){
				scene = new Scene();
				scene.setCheckModel(sModel.getCheckModel());
				scene.setSceneName(sModel.getSceneName());
				scene.setSceneType(sModel.getSceneType());
				scene.setOpenFlag(sModel.getOpenFlag());
			
			//1.添加 时间条件
			SceneTimeDelayConditions delayTimeC = null;
			SceneTimeAgingConditions agingTimeC = null;
			SceneTimeDimmingConditions dimmingTimeC = null;
			for(ConditionTimeModel ctm : sModel.getTimeModelList()){
				if(ctm.getSceneName().endsWith(sModel.getSceneName())){
					switch (ctm.getConditionSubType()) {
					case 0://延时
						delayTimeC = new SceneTimeDelayConditions();
						//delayTimeC.setStartTime(ctm.getDelayStartTime());
                        delayTimeC.setStartTime(System.currentTimeMillis());
						delayTimeC.setDelayTime(ctm.getDelayDelayTime());
						scene.getConditionList().add(delayTimeC);
						break;
					case 1://时效
						agingTimeC = new SceneTimeAgingConditions();
						agingTimeC.setStartTime(ctm.getAgingStartTime());
						agingTimeC.setStopTime(ctm.getAgingStopTime());
						scene.getConditionList().add(agingTimeC);
						break;
					case 2://定时
						dimmingTimeC = new SceneTimeDimmingConditions();
						dimmingTimeC.setDimmingModelType(ctm.getDimingModelType());
						dimmingTimeC.setHours(ctm.getHours());
						dimmingTimeC.setMinutes(ctm.getMinutes());
						dimmingTimeC.setSeconds(ctm.getSeconds());
						switch (ctm.getDimingModelType()) {
						case 0://每天
							break;
						case 1://每周
							dimmingTimeC.setWeekDays(ctm.getWeekDaysArray());
							break;
						case 2://每月
							dimmingTimeC.setMonthDays(ctm.getMonthDaysArray());
							break;
						default:
							break;
						}
						scene.getConditionList().add(dimmingTimeC);
						break;

					default:
						break;
					}
				}
			}
			
			//2.添加 传感条件
			SceneSensorAirConditions ariS = null;
			SceneSensorCo2Conditions co2S = null;
			SceneSensorPm25Conditions pm25C = null;
			SceneSensorLightConditions lightC = null;
			SceneSensorTemperatureHumidityConditions tempHumC = null;
			SceneSensorAlarmConditions alarmC = null;
			
			for(ConditionSensorModel csm : sModel.getSensorModelList()){
				if(csm.getSceneName().equals(sModel.getSceneName())){
					switch (csm.getConditionSubType()) {
					case 0://0 二氧化碳
						co2S = new SceneSensorCo2Conditions();
						co2S.setBoxCpuId(csm.getBoxCpuId());
						co2S.setDeviceId(csm.getDeviceId());
						co2S.setDeviceNumber(csm.getDeviceNumber());
                        co2S.setDeviceName(csm.getDeviceName());
						co2S.setCheckCO2Value(csm.getCheckCO2Value());
                        co2S.setWay(csm.getWay());
						co2S.setConditionCheckModel(csm.getConditionCheckModelArray());
						scene.getConditionList().add(co2S);
						break;
					case 1://1 空气质量
						ariS = new SceneSensorAirConditions();
						ariS.setBoxCpuId(csm.getBoxCpuId());
						ariS.setDeviceId(csm.getDeviceId());
						ariS.setDeviceNumber(csm.getDeviceNumber());
                        ariS.setWay(csm.getWay());
                        ariS.setDeviceName(csm.getDeviceName());
						ariS.setConditionCheckModel(csm.getConditionCheckModelArray());
						ariS.setCheckAirValue(csm.getCheckAirValue());
						scene.getConditionList().add(ariS);
						break;
					case 2://2 PM2.5
						pm25C = new SceneSensorPm25Conditions();
						pm25C.setBoxCpuId(csm.getBoxCpuId());
						pm25C.setDeviceId(csm.getDeviceId());
						pm25C.setDeviceNumber(csm.getDeviceNumber());
                        pm25C.setWay(csm.getWay());
                        pm25C.setDeviceName(csm.getDeviceName());
						pm25C.setConditionCheckModel(csm.getConditionCheckModelArray());
						pm25C.setCheckPM25Value(csm.getCheckPM25Value());
						scene.getConditionList().add(pm25C);
						break;
					case 3://3 光照
						lightC = new SceneSensorLightConditions();
						lightC.setBoxCpuId(csm.getBoxCpuId());
						lightC.setDeviceId(csm.getDeviceId());
						lightC.setDeviceNumber(csm.getDeviceNumber());
                        lightC.setWay(csm.getWay());
                        lightC.setDeviceName(csm.getDeviceName());
						lightC.setConditionCheckModel(csm.getConditionCheckModelArray());
						lightC.setCheckLightValue(csm.getCheckLightValue());
						scene.getConditionList().add(lightC);
						break;
					case 4://4 温湿度
						tempHumC = new SceneSensorTemperatureHumidityConditions();
						tempHumC.setBoxCpuId(csm.getBoxCpuId());
						tempHumC.setDeviceId(csm.getDeviceId());
						tempHumC.setDeviceNumber(csm.getDeviceNumber());
                        tempHumC.setWay(csm.getWay());
                        tempHumC.setDeviceName(csm.getDeviceName());
						tempHumC.setConditionCheckModel(csm.getConditionCheckModelArray());
						tempHumC.setCheckTemperatureValue(csm.getCheckTemperatureValue());
						tempHumC.setCheckHumidityValue(csm.getCheckHumidityValue());
						scene.getConditionList().add(tempHumC);
						break;
					case 5://5 风雨传感器
						
						break;
					case 6://6 报警类设备
						alarmC = new SceneSensorAlarmConditions();
						alarmC.setBoxCpuId(csm.getBoxCpuId());
						alarmC.setDeviceId(csm.getDeviceId());
						alarmC.setDeviceNumber(csm.getDeviceNumber());
                        alarmC.setWay(csm.getWay());
                        alarmC.setDeviceName(csm.getDeviceName());
						alarmC.setConditionCheckModel(csm.getConditionCheckModelArray());
						alarmC.setCheckAlarmValue(csm.getCheckAlarmValue());
						scene.getConditionList().add(alarmC);
						break;

					default:
						break;
					}
				}
			}
				
			//3.添加 状态条件
			SceneStatusOpenConditions openC = null;
			SceneStatusCloseConditions closeC = null;
			SceneStatusDimmingConditions dimmingC = null;
			
			for(ConditionStatusModel csm : sModel.getStatusModelList()){
				if(csm.getSceneName().equals(sModel.getSceneName())){
					switch (csm.getConditionSubType()) {
					case 0://打开
						openC = new SceneStatusOpenConditions();
						openC.setBoxCpuId(csm.getBoxCpuId());
						openC.setDeviceId(csm.getDeviceId());
						openC.setDeviceNumber(csm.getDeviceNumber());
                        openC.setWay(csm.getWay());
                        openC.setDeviceName(csm.getDeviceName());
						scene.getConditionList().add(openC);
						break;
					case 1://关闭
						closeC = new SceneStatusCloseConditions();
						closeC.setBoxCpuId(csm.getBoxCpuId());
						closeC.setDeviceId(csm.getDeviceId());
						closeC.setDeviceNumber(csm.getDeviceNumber());
                        closeC.setWay(csm.getWay());
                        closeC.setDeviceName(csm.getDeviceName());
						scene.getConditionList().add(closeC);
						break;
					case 2://设定到某个值
						dimmingC = new SceneStatusDimmingConditions();
						dimmingC.setBoxCpuId(csm.getBoxCpuId());
						dimmingC.setDeviceId(csm.getDeviceId());
						dimmingC.setDeviceNumber(csm.getDeviceNumber());
                        dimmingC.setWay(csm.getWay());
                        dimmingC.setDeviceName(csm.getDeviceName());
						dimmingC.setDimmingValue(csm.getDimmingValue());
						scene.getConditionList().add(dimmingC);
						break;

					default:
						break;
					}
				}
			}
			
			//4.添加 动作
			SceneCloseAction closeA = null;
			SceneOpenAction openA = null;
			SceneDimmingAction dimmingA = null;
			SceneStopAction stopA = null;
			for(ActionModel am :sModel.getActionModelList()){
				if(am.getSceneName().equals(sModel.getSceneName())){
					switch (am.getActionType()) {
					case 0://打开
						openA = new SceneOpenAction();
						openA.setBoxCpuId(am.getBoxCpuId());
						openA.setDeviceId(am.getDeviceId());
						openA.setDeviceNumber(am.getDeviceNumber());
                        openA.setWay(am.getWay());
                        openA.setDeviceName(am.getDeviceName());
						if(am.getDelayActionTime()==0){
							openA.setDelayActionTime(500);
						}else {
							openA.setDelayActionTime(am.getDelayActionTime());
						}
						openA.setOrderNumber(am.getOrderNumber());
						scene.getActionList().add(openA);
						break;
					case 1://关闭
						closeA = new SceneCloseAction();
						closeA.setBoxCpuId(am.getBoxCpuId());
						closeA.setDeviceId(am.getDeviceId());
						closeA.setDeviceNumber(am.getDeviceNumber());
                        closeA.setWay(am.getWay());
                        closeA.setDeviceName(am.getDeviceName());
						if(am.getDelayActionTime()==0){
							closeA.setDelayActionTime(500);
						}else {
							closeA.setDelayActionTime(am.getDelayActionTime());
						}
						closeA.setOrderNumber(am.getOrderNumber());
						scene.getActionList().add(closeA);
						break;
					case 2://调值
						dimmingA = new SceneDimmingAction();
						dimmingA.setBoxCpuId(am.getBoxCpuId());
						dimmingA.setDeviceId(am.getDeviceId());
						dimmingA.setDeviceNumber(am.getDeviceNumber());
                        dimmingA.setWay(am.getWay());
                        dimmingA.setDeviceName(am.getDeviceName());
						if(am.getDelayActionTime()==0){
							dimmingA.setDelayActionTime(500);
						}else {
							dimmingA.setDelayActionTime(am.getDelayActionTime());
						}
						dimmingA.setDimmingValue(am.getDimmingValue());
						dimmingA.setOrderNumber(am.getOrderNumber());
						scene.getActionList().add(dimmingA);
						break;
					case 3://停止
						stopA = new SceneStopAction();
						stopA.setBoxCpuId(am.getBoxCpuId());
						stopA.setDeviceId(am.getDeviceId());
						stopA.setDeviceNumber(am.getDeviceNumber());
                        stopA.setWay(am.getWay());
                        stopA.setDeviceName(am.getDeviceName());
						if(am.getDelayActionTime()==0){
							stopA.setDelayActionTime(500);
						}else {
							stopA.setDelayActionTime(am.getDelayActionTime());
						}
						stopA.setOrderNumber(am.getOrderNumber());
						scene.getActionList().add(stopA);
						break;

					default:
						break;
					}
				}
			}

			//添加红外家电动作
			SceneInfraredAction infraredAction = null;
			for(ActionInfraredModel aim : sModel.getActionInfraredModelList()){
				if(aim.getSceneName().equals(sModel.getSceneName())){
					switch (aim.getActionType()){
						case 4://家电
							infraredAction = new SceneInfraredAction();
							infraredAction.setMachineName(aim.getMachineName());
							infraredAction.setButtonId(aim.getButtonId());
							if(aim.getDelayActionTime()==0){
								infraredAction.setDelayActionTime(500);
							}else {
								infraredAction.setDelayActionTime(aim.getDelayActionTime());
							}
							infraredAction.setOrderNumber(aim.getOrderNumber());
							scene.getActionList().add(infraredAction);
							break;
						default:
							break;
					}
				}
			}

			//6.添加到服务
			addGroupScene(scene);
			Log.d(TAG, "加载情景"+scene.getSceneName());
		}
	}


	public void loadPasstiveSceneList(){
		SceneModelDao smd = new SceneModelDao(context);
		List<SceneModel> sceneModelList = smd.getList();

		Scene scene = null;
		for(SceneModel sModel : sceneModelList){
			scene = new Scene();
			scene.setCheckModel(sModel.getCheckModel());
			scene.setSceneName(sModel.getSceneName());
			scene.setSceneType(sModel.getSceneType());
			scene.setOpenFlag(sModel.getOpenFlag());

			//4.添加 动作
			SceneCloseAction closeA = null;
			SceneOpenAction openA = null;
			SceneDimmingAction dimmingA = null;
			SceneStopAction stopA = null;
			for(ActionModel am :sModel.getActionModelList()){
				if(am.getSceneName().equals(sModel.getSceneName())){
					switch (am.getActionType()) {
						case 0://打开
							openA = new SceneOpenAction();
							openA.setBoxCpuId(am.getBoxCpuId());
							openA.setDeviceId(am.getDeviceId());
							openA.setDeviceNumber(am.getDeviceNumber());
							openA.setWay(am.getWay());
							openA.setDeviceName(am.getDeviceName());
							openA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(openA);
							break;
						case 1://关闭
							closeA = new SceneCloseAction();
							closeA.setBoxCpuId(am.getBoxCpuId());
							closeA.setDeviceId(am.getDeviceId());
							closeA.setDeviceNumber(am.getDeviceNumber());
							closeA.setWay(am.getWay());
							closeA.setDeviceName(am.getDeviceName());
							closeA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(closeA);
							break;
						case 2://调值
							dimmingA = new SceneDimmingAction();
							dimmingA.setBoxCpuId(am.getBoxCpuId());
							dimmingA.setDeviceId(am.getDeviceId());
							dimmingA.setDeviceNumber(am.getDeviceNumber());
							dimmingA.setWay(am.getWay());
							dimmingA.setDeviceName(am.getDeviceName());
							dimmingA.setDelayActionTime(am.getDelayActionTime());
							dimmingA.setDimmingValue(am.getDimmingValue());
							scene.getActionList().add(dimmingA);
							break;
						case 3://停止
							stopA = new SceneStopAction();
							stopA.setBoxCpuId(am.getBoxCpuId());
							stopA.setDeviceId(am.getDeviceId());
							stopA.setDeviceNumber(am.getDeviceNumber());
							stopA.setWay(am.getWay());
							stopA.setDeviceName(am.getDeviceName());
							stopA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(stopA);
							break;

						default:
							break;
					}
				}
			}

			//添加红外家电动作
			SceneInfraredAction infraredAction = null;
			for(ActionInfraredModel aim : sModel.getActionInfraredModelList()){
				if(aim.getSceneName().equals(sModel.getSceneName())){
					switch (aim.getActionType()){
						case 4://家电
							infraredAction = new SceneInfraredAction();
							infraredAction.setMachineName(aim.getMachineName());
							infraredAction.setButtonId(aim.getButtonId());
							infraredAction.setDelayActionTime(aim.getDelayActionTime());
							scene.getActionList().add(infraredAction);
							break;
						default:
							break;
					}
				}
			}

			//6.添加到服务
			addPasstiveSceneMap(scene);
			Log.d(TAG, "加载主动情景 "+scene.getSceneName()+" 成功");
		}
	}


	public void loadSensorList(){
		sceneMap.clear();
		SceneModelDao smd = new SceneModelDao(context);
		List<SceneModel> sceneModelList = smd.getList();

		Scene scene = null;
		for(SceneModel sModel : sceneModelList){
			scene = new Scene();
			scene.setCheckModel(sModel.getCheckModel());
			scene.setSceneName(sModel.getSceneName());
			scene.setSceneType(sModel.getSceneType());
			scene.setOpenFlag(sModel.getOpenFlag());


			//2.添加 传感条件
			SceneSensorAirConditions ariS = null;
			SceneSensorCo2Conditions co2S = null;
			SceneSensorPm25Conditions pm25C = null;
			SceneSensorLightConditions lightC = null;
			SceneSensorTemperatureHumidityConditions tempHumC = null;
			SceneSensorAlarmConditions alarmC = null;

			for(ConditionSensorModel csm : sModel.getSensorModelList()){
				if(csm.getSceneName().equals(sModel.getSceneName())){
					switch (csm.getConditionSubType()) {
						case 0://0 二氧化碳
							co2S = new SceneSensorCo2Conditions();
							co2S.setBoxCpuId(csm.getBoxCpuId());
							co2S.setDeviceId(csm.getDeviceId());
							co2S.setDeviceNumber(csm.getDeviceNumber());
							co2S.setDeviceName(csm.getDeviceName());
							co2S.setCheckCO2Value(csm.getCheckCO2Value());
							co2S.setWay(csm.getWay());
							co2S.setConditionCheckModel(csm.getConditionCheckModelArray());
							scene.getConditionList().add(co2S);
							break;
						case 1://1 空气质量
							ariS = new SceneSensorAirConditions();
							ariS.setBoxCpuId(csm.getBoxCpuId());
							ariS.setDeviceId(csm.getDeviceId());
							ariS.setDeviceNumber(csm.getDeviceNumber());
							ariS.setWay(csm.getWay());
							ariS.setDeviceName(csm.getDeviceName());
							ariS.setConditionCheckModel(csm.getConditionCheckModelArray());
							ariS.setCheckAirValue(csm.getCheckAirValue());
							scene.getConditionList().add(ariS);
							break;
						case 2://2 PM2.5
							pm25C = new SceneSensorPm25Conditions();
							pm25C.setBoxCpuId(csm.getBoxCpuId());
							pm25C.setDeviceId(csm.getDeviceId());
							pm25C.setDeviceNumber(csm.getDeviceNumber());
							pm25C.setWay(csm.getWay());
							pm25C.setDeviceName(csm.getDeviceName());
							pm25C.setConditionCheckModel(csm.getConditionCheckModelArray());
							pm25C.setCheckPM25Value(csm.getCheckPM25Value());
							scene.getConditionList().add(pm25C);
							break;
						case 3://3 光照
							lightC = new SceneSensorLightConditions();
							lightC.setBoxCpuId(csm.getBoxCpuId());
							lightC.setDeviceId(csm.getDeviceId());
							lightC.setDeviceNumber(csm.getDeviceNumber());
							lightC.setWay(csm.getWay());
							lightC.setDeviceName(csm.getDeviceName());
							lightC.setConditionCheckModel(csm.getConditionCheckModelArray());
							lightC.setCheckLightValue(csm.getCheckLightValue());
							scene.getConditionList().add(lightC);
							break;
						case 4://4 温湿度
							tempHumC = new SceneSensorTemperatureHumidityConditions();
							tempHumC.setBoxCpuId(csm.getBoxCpuId());
							tempHumC.setDeviceId(csm.getDeviceId());
							tempHumC.setDeviceNumber(csm.getDeviceNumber());
							tempHumC.setWay(csm.getWay());
							tempHumC.setDeviceName(csm.getDeviceName());
							tempHumC.setConditionCheckModel(csm.getConditionCheckModelArray());
							tempHumC.setCheckTemperatureValue(csm.getCheckTemperatureValue());
							tempHumC.setCheckHumidityValue(csm.getCheckHumidityValue());
							scene.getConditionList().add(tempHumC);
							break;
						case 5://5 风雨传感器

							break;
						case 6://6 报警类设备
							alarmC = new SceneSensorAlarmConditions();
							alarmC.setBoxCpuId(csm.getBoxCpuId());
							alarmC.setDeviceId(csm.getDeviceId());
							alarmC.setDeviceNumber(csm.getDeviceNumber());
							alarmC.setWay(csm.getWay());
							alarmC.setDeviceName(csm.getDeviceName());
							alarmC.setConditionCheckModel(csm.getConditionCheckModelArray());
							alarmC.setCheckAlarmValue(csm.getCheckAlarmValue());
							scene.getConditionList().add(alarmC);
							break;

						default:
							break;
					}
				}
			}

			//4.添加 动作
			SceneCloseAction closeA = null;
			SceneOpenAction openA = null;
			SceneDimmingAction dimmingA = null;
			SceneStopAction stopA = null;
			for(ActionModel am :sModel.getActionModelList()){
				if(am.getSceneName().equals(sModel.getSceneName())){
					switch (am.getActionType()) {
						case 0://打开
							openA = new SceneOpenAction();
							openA.setBoxCpuId(am.getBoxCpuId());
							openA.setDeviceId(am.getDeviceId());
							openA.setDeviceNumber(am.getDeviceNumber());
							openA.setWay(am.getWay());
							openA.setDeviceName(am.getDeviceName());
							openA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(openA);
							break;
						case 1://关闭
							closeA = new SceneCloseAction();
							closeA.setBoxCpuId(am.getBoxCpuId());
							closeA.setDeviceId(am.getDeviceId());
							closeA.setDeviceNumber(am.getDeviceNumber());
							closeA.setWay(am.getWay());
							closeA.setDeviceName(am.getDeviceName());
							closeA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(closeA);
							break;
						case 2://调值
							dimmingA = new SceneDimmingAction();
							dimmingA.setBoxCpuId(am.getBoxCpuId());
							dimmingA.setDeviceId(am.getDeviceId());
							dimmingA.setDeviceNumber(am.getDeviceNumber());
							dimmingA.setWay(am.getWay());
							dimmingA.setDeviceName(am.getDeviceName());
							dimmingA.setDelayActionTime(am.getDelayActionTime());
							dimmingA.setDimmingValue(am.getDimmingValue());
							scene.getActionList().add(dimmingA);
							break;
						case 3://停止
							stopA = new SceneStopAction();
							stopA.setBoxCpuId(am.getBoxCpuId());
							stopA.setDeviceId(am.getDeviceId());
							stopA.setDeviceNumber(am.getDeviceNumber());
							stopA.setWay(am.getWay());
							stopA.setDeviceName(am.getDeviceName());
							stopA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(stopA);
							break;

						default:
							break;
					}
				}
			}

			//添加红外家电动作
			SceneInfraredAction infraredAction = null;
			for(ActionInfraredModel aim : sModel.getActionInfraredModelList()){
				if(aim.getSceneName().equals(sModel.getSceneName())){
					switch (aim.getActionType()){
						case 4://家电
							infraredAction = new SceneInfraredAction();
							infraredAction.setMachineName(aim.getMachineName());
							infraredAction.setButtonId(aim.getButtonId());
							infraredAction.setDelayActionTime(aim.getDelayActionTime());
							scene.getActionList().add(infraredAction);
							break;
						default:
							break;
					}
				}
			}

			//6.添加到服务
			addScene(scene);
			Log.d(TAG, "加载传感情景"+scene.getSceneName());
		}
	}


	public void loadStatusList(){
		statusSceneMap.clear();
		SceneModelDao smd = new SceneModelDao(context);
		List<SceneModel> sceneModelList = smd.getList();

		Scene scene = null;
		for(SceneModel sModel : sceneModelList){
			scene = new Scene();
			scene.setCheckModel(sModel.getCheckModel());
			scene.setSceneName(sModel.getSceneName());
			scene.setSceneType(sModel.getSceneType());
			scene.setOpenFlag(sModel.getOpenFlag());

			//3.添加 状态条件
			SceneStatusOpenConditions openC = null;
			SceneStatusCloseConditions closeC = null;
			SceneStatusDimmingConditions dimmingC = null;

			for(ConditionStatusModel csm : sModel.getStatusModelList()){
				if(csm.getSceneName().equals(sModel.getSceneName())){
					switch (csm.getConditionSubType()) {
						case 0://打开
							openC = new SceneStatusOpenConditions();
							openC.setBoxCpuId(csm.getBoxCpuId());
							openC.setDeviceId(csm.getDeviceId());
							openC.setDeviceNumber(csm.getDeviceNumber());
							openC.setWay(csm.getWay());
							openC.setDeviceName(csm.getDeviceName());
							scene.getConditionList().add(openC);
							break;
						case 1://关闭
							closeC = new SceneStatusCloseConditions();
							closeC.setBoxCpuId(csm.getBoxCpuId());
							closeC.setDeviceId(csm.getDeviceId());
							closeC.setDeviceNumber(csm.getDeviceNumber());
							closeC.setWay(csm.getWay());
							closeC.setDeviceName(csm.getDeviceName());
							scene.getConditionList().add(closeC);
							break;
						case 2://设定到某个值
							dimmingC = new SceneStatusDimmingConditions();
							dimmingC.setBoxCpuId(csm.getBoxCpuId());
							dimmingC.setDeviceId(csm.getDeviceId());
							dimmingC.setDeviceNumber(csm.getDeviceNumber());
							dimmingC.setWay(csm.getWay());
							dimmingC.setDeviceName(csm.getDeviceName());
							dimmingC.setDimmingValue(csm.getDimmingValue());
							scene.getConditionList().add(dimmingC);
							break;

						default:
							break;
					}
				}
			}

			//4.添加 动作
			SceneCloseAction closeA = null;
			SceneOpenAction openA = null;
			SceneDimmingAction dimmingA = null;
			SceneStopAction stopA = null;
			for(ActionModel am :sModel.getActionModelList()){
				if(am.getSceneName().equals(sModel.getSceneName())){
					switch (am.getActionType()) {
						case 0://打开
							openA = new SceneOpenAction();
							openA.setBoxCpuId(am.getBoxCpuId());
							openA.setDeviceId(am.getDeviceId());
							openA.setDeviceNumber(am.getDeviceNumber());
							openA.setWay(am.getWay());
							openA.setDeviceName(am.getDeviceName());
							openA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(openA);
							break;
						case 1://关闭
							closeA = new SceneCloseAction();
							closeA.setBoxCpuId(am.getBoxCpuId());
							closeA.setDeviceId(am.getDeviceId());
							closeA.setDeviceNumber(am.getDeviceNumber());
							closeA.setWay(am.getWay());
							closeA.setDeviceName(am.getDeviceName());
							closeA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(closeA);
							break;
						case 2://调值
							dimmingA = new SceneDimmingAction();
							dimmingA.setBoxCpuId(am.getBoxCpuId());
							dimmingA.setDeviceId(am.getDeviceId());
							dimmingA.setDeviceNumber(am.getDeviceNumber());
							dimmingA.setWay(am.getWay());
							dimmingA.setDeviceName(am.getDeviceName());
							dimmingA.setDelayActionTime(am.getDelayActionTime());
							dimmingA.setDimmingValue(am.getDimmingValue());
							scene.getActionList().add(dimmingA);
							break;
						case 3://停止
							stopA = new SceneStopAction();
							stopA.setBoxCpuId(am.getBoxCpuId());
							stopA.setDeviceId(am.getDeviceId());
							stopA.setDeviceNumber(am.getDeviceNumber());
							stopA.setWay(am.getWay());
							stopA.setDeviceName(am.getDeviceName());
							stopA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(stopA);
							break;

						default:
							break;
					}
				}
			}

			//添加红外家电动作
			SceneInfraredAction infraredAction = null;
			for(ActionInfraredModel aim : sModel.getActionInfraredModelList()){
				if(aim.getSceneName().equals(sModel.getSceneName())){
					switch (aim.getActionType()){
						case 4://家电
							infraredAction = new SceneInfraredAction();
							infraredAction.setMachineName(aim.getMachineName());
							infraredAction.setButtonId(aim.getButtonId());
							infraredAction.setDelayActionTime(aim.getDelayActionTime());
							scene.getActionList().add(infraredAction);
							break;
						default:
							break;
					}
				}
			}

			//6.添加到服务
			addStatusScene(scene);
			Log.d(TAG, "加载状态情景"+scene.getSceneName());
		}
	}


	public void loadTimeList(){
		timeSceneMap.clear();
		SceneModelDao smd = new SceneModelDao(context);
		List<SceneModel> sceneModelList = smd.getList();

		Scene scene = null;
		for(SceneModel sModel : sceneModelList){
			scene = new Scene();
			scene.setCheckModel(sModel.getCheckModel());
			scene.setSceneName(sModel.getSceneName());
			scene.setSceneType(sModel.getSceneType());
			scene.setOpenFlag(sModel.getOpenFlag());

			//1.添加 时间条件
			SceneTimeDelayConditions delayTimeC = null;
			SceneTimeAgingConditions agingTimeC = null;
			SceneTimeDimmingConditions dimmingTimeC = null;
			for(ConditionTimeModel ctm : sModel.getTimeModelList()){
				if(ctm.getSceneName().endsWith(sModel.getSceneName())){
					switch (ctm.getConditionSubType()) {
						case 0://延时
							delayTimeC = new SceneTimeDelayConditions();
							//delayTimeC.setStartTime(ctm.getDelayStartTime());
							delayTimeC.setStartTime(System.currentTimeMillis());
							delayTimeC.setDelayTime(ctm.getDelayDelayTime());
							scene.getConditionList().add(delayTimeC);
							break;
						case 1://时效
							agingTimeC = new SceneTimeAgingConditions();
							agingTimeC.setStartTime(ctm.getAgingStartTime());
							agingTimeC.setStopTime(ctm.getAgingStopTime());
							scene.getConditionList().add(agingTimeC);
							break;
						case 2://定时
							dimmingTimeC = new SceneTimeDimmingConditions();
							dimmingTimeC.setDimmingModelType(ctm.getDimingModelType());
							dimmingTimeC.setHours(ctm.getHours());
							dimmingTimeC.setMinutes(ctm.getMinutes());
							dimmingTimeC.setSeconds(ctm.getSeconds());
							switch (ctm.getDimingModelType()) {
								case 0://每天
									break;
								case 1://每周
									dimmingTimeC.setWeekDays(ctm.getWeekDaysArray());
									break;
								case 2://每月
									dimmingTimeC.setMonthDays(ctm.getMonthDaysArray());
									break;
								default:
									break;
							}
							scene.getConditionList().add(dimmingTimeC);
							break;

						default:
							break;
					}
				}
			}

			//4.添加 动作
			SceneCloseAction closeA = null;
			SceneOpenAction openA = null;
			SceneDimmingAction dimmingA = null;
			SceneStopAction stopA = null;
			for(ActionModel am :sModel.getActionModelList()){
				if(am.getSceneName().equals(sModel.getSceneName())){
					switch (am.getActionType()) {
						case 0://打开
							openA = new SceneOpenAction();
							openA.setBoxCpuId(am.getBoxCpuId());
							openA.setDeviceId(am.getDeviceId());
							openA.setDeviceNumber(am.getDeviceNumber());
							openA.setWay(am.getWay());
							openA.setDeviceName(am.getDeviceName());
							openA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(openA);
							break;
						case 1://关闭
							closeA = new SceneCloseAction();
							closeA.setBoxCpuId(am.getBoxCpuId());
							closeA.setDeviceId(am.getDeviceId());
							closeA.setDeviceNumber(am.getDeviceNumber());
							closeA.setWay(am.getWay());
							closeA.setDeviceName(am.getDeviceName());
							closeA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(closeA);
							break;
						case 2://调值
							dimmingA = new SceneDimmingAction();
							dimmingA.setBoxCpuId(am.getBoxCpuId());
							dimmingA.setDeviceId(am.getDeviceId());
							dimmingA.setDeviceNumber(am.getDeviceNumber());
							dimmingA.setWay(am.getWay());
							dimmingA.setDeviceName(am.getDeviceName());
							dimmingA.setDelayActionTime(am.getDelayActionTime());
							dimmingA.setDimmingValue(am.getDimmingValue());
							scene.getActionList().add(dimmingA);
							break;
						case 3://停止
							stopA = new SceneStopAction();
							stopA.setBoxCpuId(am.getBoxCpuId());
							stopA.setDeviceId(am.getDeviceId());
							stopA.setDeviceNumber(am.getDeviceNumber());
							stopA.setWay(am.getWay());
							stopA.setDeviceName(am.getDeviceName());
							stopA.setDelayActionTime(am.getDelayActionTime());
							scene.getActionList().add(stopA);
							break;

						default:
							break;
					}
				}
			}

			//添加红外家电动作
			SceneInfraredAction infraredAction = null;
			for(ActionInfraredModel aim : sModel.getActionInfraredModelList()){
				if(aim.getSceneName().equals(sModel.getSceneName())){
					switch (aim.getActionType()){
						case 4://家电
							infraredAction = new SceneInfraredAction();
							infraredAction.setMachineName(aim.getMachineName());
							infraredAction.setButtonId(aim.getButtonId());
							infraredAction.setDelayActionTime(aim.getDelayActionTime());
							scene.getActionList().add(infraredAction);
							break;
						default:
							break;
					}
				}
			}

			//6.添加到服务
			addTimeScene(scene);
			Log.d(TAG, "加载时间情景"+scene.getSceneName());
		}
	}

    public void loadAllSceneList(){
        SceneModelDao smd = new SceneModelDao(context);
        List<SceneModel> sceneModelList = smd.getList();

        Scene scene = null;
        for(SceneModel sModel : sceneModelList){
            scene = new Scene();
            scene.setCheckModel(sModel.getCheckModel());
            scene.setSceneName(sModel.getSceneName());
            scene.setSceneType(sModel.getSceneType());
            scene.setOpenFlag(sModel.getOpenFlag());

            //1.添加 时间条件
            SceneTimeDelayConditions delayTimeC = null;
            SceneTimeAgingConditions agingTimeC = null;
            SceneTimeDimmingConditions dimmingTimeC = null;
            for(ConditionTimeModel ctm : sModel.getTimeModelList()){
                if(ctm.getSceneName().endsWith(sModel.getSceneName())){
                    switch (ctm.getConditionSubType()) {
                        case 0://延时
                            delayTimeC = new SceneTimeDelayConditions();
                            //delayTimeC.setStartTime(ctm.getDelayStartTime());
                            delayTimeC.setStartTime(System.currentTimeMillis());
                            delayTimeC.setDelayTime(ctm.getDelayDelayTime());
                            scene.getConditionList().add(delayTimeC);
                            break;
                        case 1://时效
                            agingTimeC = new SceneTimeAgingConditions();
                            agingTimeC.setStartTime(ctm.getAgingStartTime());
                            agingTimeC.setStopTime(ctm.getAgingStopTime());
                            scene.getConditionList().add(agingTimeC);
                            break;
                        case 2://定时
                            dimmingTimeC = new SceneTimeDimmingConditions();
                            dimmingTimeC.setDimmingModelType(ctm.getDimingModelType());
                            dimmingTimeC.setHours(ctm.getHours());
                            dimmingTimeC.setMinutes(ctm.getMinutes());
                            dimmingTimeC.setSeconds(ctm.getSeconds());
                            switch (ctm.getDimingModelType()) {
                                case 0://每天
                                    break;
                                case 1://每周
                                    dimmingTimeC.setWeekDays(ctm.getWeekDaysArray());
                                    break;
                                case 2://每月
                                    dimmingTimeC.setMonthDays(ctm.getMonthDaysArray());
                                    break;
                                default:
                                    break;
                            }
                            scene.getConditionList().add(dimmingTimeC);
                            break;

                        default:
                            break;
                    }
                }
            }

            //2.添加 传感条件
            SceneSensorAirConditions ariS = null;
            SceneSensorCo2Conditions co2S = null;
            SceneSensorPm25Conditions pm25C = null;
            SceneSensorLightConditions lightC = null;
            SceneSensorTemperatureHumidityConditions tempHumC = null;
            SceneSensorAlarmConditions alarmC = null;

            for(ConditionSensorModel csm : sModel.getSensorModelList()){
                if(csm.getSceneName().equals(sModel.getSceneName())){
                    switch (csm.getConditionSubType()) {
                        case 0://0 二氧化碳
                            co2S = new SceneSensorCo2Conditions();
                            co2S.setBoxCpuId(csm.getBoxCpuId());
                            co2S.setDeviceId(csm.getDeviceId());
                            co2S.setDeviceNumber(csm.getDeviceNumber());
                            co2S.setDeviceName(csm.getDeviceName());
                            co2S.setCheckCO2Value(csm.getCheckCO2Value());
                            co2S.setWay(csm.getWay());
                            co2S.setConditionCheckModel(csm.getConditionCheckModelArray());
                            scene.getConditionList().add(co2S);
                            break;
                        case 1://1 空气质量
                            ariS = new SceneSensorAirConditions();
                            ariS.setBoxCpuId(csm.getBoxCpuId());
                            ariS.setDeviceId(csm.getDeviceId());
                            ariS.setDeviceNumber(csm.getDeviceNumber());
                            ariS.setWay(csm.getWay());
                            ariS.setDeviceName(csm.getDeviceName());
                            ariS.setConditionCheckModel(csm.getConditionCheckModelArray());
                            ariS.setCheckAirValue(csm.getCheckAirValue());
                            scene.getConditionList().add(ariS);
                            break;
                        case 2://2 PM2.5
                            pm25C = new SceneSensorPm25Conditions();
                            pm25C.setBoxCpuId(csm.getBoxCpuId());
                            pm25C.setDeviceId(csm.getDeviceId());
                            pm25C.setDeviceNumber(csm.getDeviceNumber());
                            pm25C.setWay(csm.getWay());
                            pm25C.setDeviceName(csm.getDeviceName());
                            pm25C.setConditionCheckModel(csm.getConditionCheckModelArray());
                            pm25C.setCheckPM25Value(csm.getCheckPM25Value());
                            scene.getConditionList().add(pm25C);
                            break;
                        case 3://3 光照
                            lightC = new SceneSensorLightConditions();
                            lightC.setBoxCpuId(csm.getBoxCpuId());
                            lightC.setDeviceId(csm.getDeviceId());
                            lightC.setDeviceNumber(csm.getDeviceNumber());
                            lightC.setWay(csm.getWay());
                            lightC.setDeviceName(csm.getDeviceName());
                            lightC.setConditionCheckModel(csm.getConditionCheckModelArray());
                            lightC.setCheckLightValue(csm.getCheckLightValue());
                            scene.getConditionList().add(lightC);
                            break;
                        case 4://4 温湿度
                            tempHumC = new SceneSensorTemperatureHumidityConditions();
                            tempHumC.setBoxCpuId(csm.getBoxCpuId());
                            tempHumC.setDeviceId(csm.getDeviceId());
                            tempHumC.setDeviceNumber(csm.getDeviceNumber());
                            tempHumC.setWay(csm.getWay());
                            tempHumC.setDeviceName(csm.getDeviceName());
                            tempHumC.setConditionCheckModel(csm.getConditionCheckModelArray());
                            tempHumC.setCheckTemperatureValue(csm.getCheckTemperatureValue());
                            tempHumC.setCheckHumidityValue(csm.getCheckHumidityValue());
                            scene.getConditionList().add(tempHumC);
                            break;
                        case 5://5 风雨传感器

                            break;
                        case 6://6 报警类设备
                            alarmC = new SceneSensorAlarmConditions();
                            alarmC.setBoxCpuId(csm.getBoxCpuId());
                            alarmC.setDeviceId(csm.getDeviceId());
                            alarmC.setDeviceNumber(csm.getDeviceNumber());
                            alarmC.setWay(csm.getWay());
                            alarmC.setDeviceName(csm.getDeviceName());
                            alarmC.setConditionCheckModel(csm.getConditionCheckModelArray());
                            alarmC.setCheckAlarmValue(csm.getCheckAlarmValue());
                            scene.getConditionList().add(alarmC);
                            break;

                        default:
                            break;
                    }
                }
            }

            //3.添加 状态条件
            SceneStatusOpenConditions openC = null;
            SceneStatusCloseConditions closeC = null;
            SceneStatusDimmingConditions dimmingC = null;

            for(ConditionStatusModel csm : sModel.getStatusModelList()){
                if(csm.getSceneName().equals(sModel.getSceneName())){
                    switch (csm.getConditionSubType()) {
                        case 0://打开
                            openC = new SceneStatusOpenConditions();
                            openC.setBoxCpuId(csm.getBoxCpuId());
                            openC.setDeviceId(csm.getDeviceId());
                            openC.setDeviceNumber(csm.getDeviceNumber());
                            openC.setWay(csm.getWay());
                            openC.setDeviceName(csm.getDeviceName());
                            scene.getConditionList().add(openC);
                            break;
                        case 1://关闭
                            closeC = new SceneStatusCloseConditions();
                            closeC.setBoxCpuId(csm.getBoxCpuId());
                            closeC.setDeviceId(csm.getDeviceId());
                            closeC.setDeviceNumber(csm.getDeviceNumber());
                            closeC.setWay(csm.getWay());
                            closeC.setDeviceName(csm.getDeviceName());
                            scene.getConditionList().add(closeC);
                            break;
                        case 2://设定到某个值
                            dimmingC = new SceneStatusDimmingConditions();
                            dimmingC.setBoxCpuId(csm.getBoxCpuId());
                            dimmingC.setDeviceId(csm.getDeviceId());
                            dimmingC.setDeviceNumber(csm.getDeviceNumber());
                            dimmingC.setWay(csm.getWay());
                            dimmingC.setDeviceName(csm.getDeviceName());
                            dimmingC.setDimmingValue(csm.getDimmingValue());
                            scene.getConditionList().add(dimmingC);
                            break;

                        default:
                            break;
                    }
                }
            }

            //4.添加 动作
            SceneCloseAction closeA = null;
            SceneOpenAction openA = null;
            SceneDimmingAction dimmingA = null;
            SceneStopAction stopA = null;
            for(ActionModel am :sModel.getActionModelList()){
                if(am.getSceneName().equals(sModel.getSceneName())){
                    switch (am.getActionType()) {
                        case 0://打开
                            openA = new SceneOpenAction();
                            openA.setBoxCpuId(am.getBoxCpuId());
                            openA.setDeviceId(am.getDeviceId());
                            openA.setDeviceNumber(am.getDeviceNumber());
                            openA.setWay(am.getWay());
                            openA.setDeviceName(am.getDeviceName());
                            openA.setDelayActionTime(am.getDelayActionTime());
							openA.setOrderNumber(am.getOrderNumber());
                            scene.getActionList().add(openA);
                            break;
                        case 1://关闭
                            closeA = new SceneCloseAction();
                            closeA.setBoxCpuId(am.getBoxCpuId());
                            closeA.setDeviceId(am.getDeviceId());
                            closeA.setDeviceNumber(am.getDeviceNumber());
                            closeA.setWay(am.getWay());
                            closeA.setDeviceName(am.getDeviceName());
                            closeA.setDelayActionTime(am.getDelayActionTime());
							openA.setOrderNumber(am.getOrderNumber());
                            scene.getActionList().add(closeA);
                            break;
                        case 2://调值
                            dimmingA = new SceneDimmingAction();
                            dimmingA.setBoxCpuId(am.getBoxCpuId());
                            dimmingA.setDeviceId(am.getDeviceId());
                            dimmingA.setDeviceNumber(am.getDeviceNumber());
                            dimmingA.setWay(am.getWay());
                            dimmingA.setDeviceName(am.getDeviceName());
                            dimmingA.setDelayActionTime(am.getDelayActionTime());
                            dimmingA.setDimmingValue(am.getDimmingValue());
							openA.setOrderNumber(am.getOrderNumber());
                            scene.getActionList().add(dimmingA);
                            break;
                        case 3://停止
                            stopA = new SceneStopAction();
                            stopA.setBoxCpuId(am.getBoxCpuId());
                            stopA.setDeviceId(am.getDeviceId());
                            stopA.setDeviceNumber(am.getDeviceNumber());
                            stopA.setWay(am.getWay());
                            stopA.setDeviceName(am.getDeviceName());
                            stopA.setDelayActionTime(am.getDelayActionTime());
							openA.setOrderNumber(am.getOrderNumber());
                            scene.getActionList().add(stopA);
                            break;

                        default:
                            break;
                    }
                }
            }

            //添加红外家电动作
            SceneInfraredAction infraredAction = null;
            for(ActionInfraredModel aim : sModel.getActionInfraredModelList()){
                if(aim.getSceneName().equals(sModel.getSceneName())){
                    switch (aim.getActionType()){
                        case 4://家电
                            infraredAction = new SceneInfraredAction();
                            infraredAction.setMachineName(aim.getMachineName());
                            infraredAction.setButtonId(aim.getButtonId());
                            infraredAction.setDelayActionTime(aim.getDelayActionTime());
							infraredAction.setOrderNumber(aim.getOrderNumber());
                            scene.getActionList().add(infraredAction);
                            break;
                        default:
                            break;
                    }
                }
            }

            //6.添加到服务
            addGroupScene(scene);
            Log.d(TAG, "加载组合情景"+scene.getSceneName());
        }
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }



}
